package swb.api.com.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import swb.api.com.repository.dto.WithdrawAccount;
import swb.api.com.service.WithdrawAccountService;
import swb.api.com.vo.Result;
import swb.api.com.vo.req.BindAccountRequest;
import swb.api.com.vo.resp.BindAccountResponse;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 提现账户管理控制器
 * 提供用户绑定、查询、设置默认和解绑提现账户的相关接口
 */
@RestController
@RequestMapping("/api/account")
@Api(tags = "提现账户管理接口")
public class WithdrawAccountController {

    @Autowired
    private WithdrawAccountService withdrawAccountService;

    /**
     * 绑定提现账户
     * @param userId 用户ID，从请求头中获取
     * @param request 绑定账户请求参数，包含账户类型和账户信息
     * @return 绑定结果，包含是否成功和相关信息
     */
    @PostMapping("/bind")
    @ApiOperation(value = "绑定提现账户", notes = "用户绑定微信或支付宝提现账户")
    public Result<BindAccountResponse> bindAccount(
            @ApiParam(value = "用户ID", required = true) @RequestHeader("User-Id") Long userId,
            @ApiParam(value = "绑定账户请求参数", required = true) @RequestBody BindAccountRequest request) {
        BindAccountResponse response = withdrawAccountService.bindAccount(userId, request);
        if (response.getSuccess()) {
            return Result.success(response, "绑定成功");
        } else {
            return Result.error(response.getMessage());
        }
    }

    /**
     * 获取用户所有提现账户
     * @param userId 用户ID，从请求头中获取
     * @return 用户的所有提现账户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取用户提现账户列表", notes = "查询用户绑定的所有提现账户信息")
    public Result<List<WithdrawAccount>> getUserAccounts(
            @ApiParam(value = "用户ID", required = true) @RequestHeader("User-Id") Long userId) {
        List<WithdrawAccount> accounts = withdrawAccountService.getUserAccounts(userId);
        return Result.success(accounts, "查询成功");
    }

    /**
     * 设置默认账户
     * @param userId 用户ID，从请求头中获取
     * @param accountId 要设置为默认的账户ID
     * @return 设置结果，成功或失败信息
     */
    @PostMapping("/set-default/{accountId}")
    @ApiOperation(value = "设置默认提现账户", notes = "将指定账户设为用户默认提现账户")
    public Result<String> setDefaultAccount(
            @ApiParam(value = "用户ID", required = true) @RequestHeader("User-Id") Long userId,
            @ApiParam(value = "账户ID", required = true) @PathVariable Long accountId) {
        boolean success = withdrawAccountService.setDefaultAccount(userId, accountId);
        if (success) {
            return Result.success("设置成功", "默认账户设置成功");
        } else {
            return Result.error("账户不存在或已被禁用");
        }
    }

    /**
     * 解绑账户
     * @param userId 用户ID，从请求头中获取
     * @param accountId 要解绑的账户ID
     * @return 解绑结果，成功或失败信息
     */
    @DeleteMapping("/unbind/{accountId}")
    @ApiOperation(value = "解绑提现账户", notes = "解绑指定的提现账户（逻辑删除）")
    public Result<String> unbindAccount(
            @ApiParam(value = "用户ID", required = true) @RequestHeader("User-Id") Long userId,
            @ApiParam(value = "账户ID", required = true) @PathVariable Long accountId) {
        boolean success = withdrawAccountService.unbindAccount(userId, accountId);
        if (success) {
            return Result.success("解绑成功", "账户解绑成功");
        } else {
            return Result.error("账户不存在或已被禁用");
        }
    }

    /**
     * 禁用指定的提现账户
     * @param userId 用户ID，从请求头中获取
     * @param accountId 要禁用的账户ID，从路径变量中获取
     * @return 操作结果，成功或失败信息
     */
    @DeleteMapping("/disable/{accountId}")
    @ApiOperation(value = "禁用提现账户", notes = "禁用指定的提现账户")
    public Result<String> disableAccount(
            @ApiParam(value = "用户ID", required = true) @RequestHeader("User-Id") Long userId,
            @ApiParam(value = "账户ID", required = true) @PathVariable Long accountId) {
        try {
            // 调用Repository方法禁用账户
            boolean result = withdrawAccountService.disableAccount(userId, accountId);

            if (result) {
                return Result.success("禁用成功", "账户已成功禁用");
            } else {
                return Result.error("账户不存在或已被禁用");
            }
        } catch (Exception e) {
            return Result.error("禁用账户失败: " + e.getMessage());
        }
    }

}
